<template>
  <FModal width="800" ref="refFModal" @ok="ok" :header="modalTitle">
    <div style="padding-right: 30px">
      <el-form ref="refForm" label-width="120px" :model="positionData">
        <el-form-item prop="positionName" label="岗位名称" :rules="[required]">
          <el-input v-focus placeholder="请输入岗位名称" v-model="positionData.positionName"/>
        </el-form-item>

        <el-form-item prop="positionCode" label="岗位编码" :rules="[required]">
          <el-input placeholder="请输入岗位编码" v-model="positionData.positionCode"/>
        </el-form-item>

        <el-form-item prop="sort" label="排序号" :rules="[required]">
          <el-input-number style="width: 100%" placeholder="请输入排序号"
                           v-model="positionData.sort"/>
        </el-form-item>

        <el-form-item prop="positionStatus" label="状态" :rules="[required]">
          <SystemDict v-model="positionData.positionStatus" dict-code="positionStatus"/>
        </el-form-item>

        <el-form-item prop="positionDesc" label="描述" :rules="[]">
          <el-input type="textarea" placeholder="请输入描述" v-model="positionData.positionDesc"/>
        </el-form-item>
      </el-form>
    </div>
  </FModal>
</template>
<script setup>

import FModal from '@/components/FModal/index.vue';
import {computed, ref, shallowRef} from 'vue';
import {isEmpty, real, required} from '@/utils/CommonUtils';
import SystemDict from '@/components/SystemDict/index.vue';
import {getAction, postAction} from '@/request/BaseRequest';
import {ElMessage} from 'element-plus';

const refForm = shallowRef();
const refFModal = shallowRef();
const loading = shallowRef(false);
const positionData = ref({});
let unitData = undefined;
const emits = defineEmits(['reloadPosition']);

function setData(_unitData, position) {
  unitData = _unitData;
  loadPosition(position);
  refFModal.value.open();
}

function loadPosition(position) {
  if (isEmpty(position)) {
    //新建
    createDefaultPosition();
  } else {
    //编辑
    positionData.value = position;
  }
}

function createDefaultPosition() {
  positionData.value = {
    positionStatus: '0',
    systemPreset: false,
    unitId: real(unitData).id
  };
  loading.value = true;
  getAction('/sys/position/loadMaxPositionSort').then(data => {
    loading.value = false;
    if (data.success) {
      positionData.value.sort = data.result;
    }
  });
}

const modalTitle = computed(() => {
  if (isEmpty(positionData.value.id)) {
    return '新建岗位';
  }
  return '编辑岗位';
});

function ok() {
  refForm.value.validate((valid, errors) => {
    if (valid) {
      doSave(real(positionData));
    }
  });
}

function doSave(data) {
  data.unitId = real(unitData).id;
  loading.value = true;
  postAction('/sys/position/save', data).then(data => {
    loading.value = false;
    if (data.success) {
      ElMessage({
        message: '保存成功',
        type: 'success',
      });
      refFModal.value.close();
      emits('reloadPosition');
    }
  });
}

defineExpose({
  setData
});
</script>
<script>
export default {
  name: 'PositionEditModal'
};
</script>
